package com.rem.concurrency.multithreading.callable;

import cn.hutool.http.HttpUtil;
import lombok.Data;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;


/**
 * @author Rem
 * @date 2021-03-03
 */

@Data
public class Demo1 implements Callable<Boolean> {

    private String url;
    private String name;

    public Demo1(String url, String name) {
        //图片地址
        this.url = url;
        //文件名
        this.name = name;
    }


    @Override
    public Boolean call() {
        HttpUtil.downloadFile(url, name);
        System.out.println("下载了文件名为：" + name);
        return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {


        Demo1 t1 = new Demo1("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png", "../../src/main/resources/1.jpg");
        Demo1 t2 = new Demo1("https://dss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=1502947746,698507370&fm=58", "../../src/main/resources/2.jpg");
        Demo1 t3 = new Demo1("https://static001.geekbang.org/resource/image/1e/46/1ed9536031d6698570ea175a7b7f9a46.png", "../../src/main/resources/3.jpg");


        //创建线程池
        ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(3,
                new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build());

        //提交执行
        Future<Boolean> submit = executorService.submit(t1);
        Future<Boolean> submit1 = executorService.submit(t2);
        Future<Boolean> submit2 = executorService.submit(t3);


        //获结果
        Boolean aBoolean = submit.get();
        Boolean aBoolean1 = submit1.get();
        Boolean aBoolean2 = submit2.get();

        //关闭服务
        executorService.shutdown();


    }
}
